package com.taglab.chic.search;

/**
 * @author Markus Kobler
 */
public class Query {

  private String searchTerms;

  private int startPage = 0;

  private int itemsPerPage = 10;

  private String language;

  public Query(String searchTerms) {
    if( searchTerms == null ) throw new IllegalArgumentException("Search terms cannot be null");
    this.searchTerms = searchTerms;
  }

  public Query(String searchTerms, int startPage, int itemsPerPage) {
    this( searchTerms );
    this.startPage = startPage;
    this.itemsPerPage = itemsPerPage;
  }

  public Query(String searchTerms, int startPage, int itemsPerPage, String language) {
    this( searchTerms );
    this.startPage = startPage;
    this.itemsPerPage = itemsPerPage;
    this.language = language;
  }

  public String getSearchTerms() {
    return searchTerms;
  }

  public int getStartPage() {
    return startPage;
  }

  public void setStartPage(int startPage) {
    this.startPage = startPage;
  }

  public int getItemsPerPage() {
    return itemsPerPage;
  }

  public void setItemsPerPage(int itemsPerPage) {
    this.itemsPerPage = itemsPerPage;
  }

  public String getLanguage() {
    return language;
  }

  public void setLanguage(String language) {
    this.language = language;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Query query = (Query) o;

    if (itemsPerPage != query.itemsPerPage) return false;
    if (startPage != query.startPage) return false;
    if (language != null ? !language.equals(query.language) : query.language != null) return false;
    return searchTerms.equals(query.searchTerms);
  }

  private transient int hashCode = -1;

  @Override
  public int hashCode() {
    if( hashCode != -1 ) return hashCode;
    int result = searchTerms.hashCode();
    result = 31 * result + startPage;
    result = 31 * result + itemsPerPage;
    result = 31 * result + (language != null ? language.hashCode() : 0);
    return hashCode = result;
  }

  @Override
  public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append("Query");
    sb.append("{searchTerms='").append(searchTerms).append('\'');
    sb.append(", startPage=").append(startPage);
    sb.append(", itemsPerPage=").append(itemsPerPage);
    if( language != null )
      sb.append(", language='").append(language).append('\'');
    sb.append('}');
    return sb.toString();
  }
  
}
